/* ===================================================================== */
/*                 AI Init Script - will init object and                 */
/*                   give control to class init script                   */
/* ===================================================================== */


/* ===================================================================== */
/*                   Dont change this point bellow                       */
/* ===================================================================== */
#include "Common\global.sqf";
#include "Common\actions.sqf";
#include "Common\functions.sqf";
/*************************************************************************/
/********************* INLINE FUNCTION ***********************************/
_fnc_clear_actionMenu = {
	_dummyAction = _agent addAction["foo", "TES\AI\Actions\goto.sqf"];
	while {_dummyAction >= 0} do {
		_agent removeAction _dummyAction;
		_dummyAction = _dummyAction - 1;
	};
};
_fnc_add_actionMenu = {
	call _fnc_clear_actionMenu;
	//_agent addAction["GoTo Chair", "TES\AI\Actions\goto.sqf"];
	if ( (_agent getVariable "Pose") != "SitChair" ) then {_agent addAction["Sit Down", "TES\AI\Common\execAction.sqf",["SitChair"] ];};
	if ( (_agent getVariable "Pose") == "SitChair" ) then {_agent addAction["Stand Up", "TES\AI\Common\execAction.sqf",["UpChair"] ];};
};
/*************************************************************************/
_fnc_remove_queue_item = {
	_action_queue set [0,false];
	_action_queue = _action_queue - [false]; // remove 1st array item;
};
/*************************************************************************/
/*************************************************************************/
waitUntil {TES_AI_Ready};
private ["_u","_agent"];
_agent = (_this select 0);

_idx = [_agent] call _fnc_add_ai;
_agent setVariable ["Command","Idle"];
_agent setVariable ["Status","Idle"];
_agent setVariable ["Pose","Stand"];
_agent addEventHandler ["killed", {(_this select 0) setVariable ['killer',(_this select 1)]}];

/*
_agent  allowDamage false; 
_agent setVariable ["TES_handDamage", 0, false];
_agent setVariable ["TES_legDamage", 0, false];
_agent setVariable ["TES_headDamage", 0, false];
_agent setVariable ["TES_bodyDamage", 0, false];
_agent addEventHandler ["HandleDamage", {_agent execVM "handleDamage.sqf"; 0}];
if ( format ["%1", (_name getVariable "TES_totalDamage")] == "<null>") 	 then {_name setVariable ["TES_totalDamage", 0, false]};
_agent setVariable ["TES_totalDamage", 0, false]; 
*/
_mygroup = createGroup civilian;
[_agent] join _mygroup ;
_dead = false;
_myObj = null; // Object need for commands
_destPosition = []; // Needed for commands
_destDirection = 0; // Needed for commands
_action_queue = [];
_running = false;
_isIdle=false;
_aborted = false;
_checkDone = false;
/*************************************************************************/
/********************* TEMP OPTIONS **************************************/
call _fnc_add_actionMenu;
/*************************************************************************/
/************** Check AI Class Type and init Movemnt script **************/
if ((_agent isKindOf "Woman") and (_agent isKindOf "Civilian")) then {
	_agent setVehicleInit "[_agent] execVM ""TES\AI\_civFemale.sqf"";";
};
processInitCommands;
/*************************************************************************/
#ifdef TES_AI_DEBUG
	_agent globalChat format ["Ready (%1)",typeOf _agent];
#endif
/**************************** MAIN LOOP **********************************/
while {true} do
{
	if (!alive _agent) then {
		if (!_dead) then {
			call _fnc_clear_actionMenu;
			_dead = true;
		};
		sleep 5;
	} else {
		if (_checkDone) then {
		// Run this while commands are running... we dont want to get stuck in waitUntil
			_busy = false;
			while {_checkDone} do {
				// hintSilent format ["%1 %2 %3",_agent getVariable "_Status", str(_busy), str(_checkDone)];
				if (!_busy) then {
					if (_agent getVariable "_Status" == "Busy") then {_busy=true;};
				};
				if (_busy) then { 
					if (_agent getVariable "_Status" == "Idle") then { _checkDone = false;}; 
				};
				sleep 0.5;
				/**** Other stuff while busy goes here ****/
			};
			_running = false;
		};
	
		_command = _agent getVariable "Command";
		if (_command != "Idle") then { // Add new command to Queue
			_action_queue = _action_queue + [[true,_command,0]]; // needed to delete item later
			_agent setVariable ["Command","Idle"];
			#ifdef TES_AI_DEBUG
				_agent globalChat format ["Add: %1",str(_action_queue)];
			#endif
		};
		if (count _action_queue > 0 ) then {
			// if (_agent getVariable "_Status" == "Idle") then {_running = false};;
			if (!_running) then {
				_isIdle = false;
				_running = true;
				if (_aborted) then { // Last command aborted .. remove child commands from queue
					_aborted=false;
					for "_i" from 1 to _delete_on_abort do {
						call _fnc_remove_queue_item;
					};
				};
				#ifdef TES_AI_DEBUG
					_agent globalChat format ["Item Queue: %1",str((_action_queue select 0))];
				#endif
				_run_command = (_action_queue select 0) select 1;
				_delete_on_abort = (_action_queue select 0) select 2;
				call _fnc_remove_queue_item;
				
				#ifdef TES_AI_DEBUG
					_agent globalChat format ["Command: %1",str(_run_command)];
				#endif
				// multiple command here if needed or in loop if that's the case
				switch (_run_command ) do {
					case ( "SitChair" ): { // This means Find, Goto, Sit
						call _fnc_clear_actionMenu;
						_action_queue = [[true,"FindChair",3]] + 
										[[true,"Goto",2]] + 
										[[true,"SetDirection",1]] + 
										[[true,"doSitChair",0]] +_action_queue;
						_running = false;
					};
					case ( "UpChair" ): { 
						call _fnc_clear_actionMenu;
						_agent setVariable ["_Command","UpChair"];
						_agent setVariable ["Pose","Stand"];
						call _fnc_add_actionMenu;
						_agent setVariable ["Status","Idle"];
						_checkDone =true;
					};
					case ( "FindChair" ): { 
						_foundObjs = nearestObjects [_agent,["FoldChair"],20];
						if (count _foundObjs > 0) then {
							_myObj = _foundObjs select 0;
							_destDirection = getDir _myObj;
							_correctedDir = _destDirection;
							if (_myObj isKindOf "FoldChair") then {	_correctedDir = ((abs _destDirection -180) + 360 ) Mod 360;};
							_destPosition = [(getPos _myObj select 0) + (sin _correctedDir)* 0.85 ,(getPos _myObj select 1) + (cos _correctedDir)* 0.85,0]; 
							_running = false;
						} else { 
							#ifdef TES_AI_DEBUG
								_agent globalChat format ["Aborted"];
							#endif
							_aborted=true;
							hint "FAIL";
							_running = false;
						};
					};
					case ( "doSitChair" ): { // real sitting (internal only)
						_agent setPos _destPosition;
						_agent setVariable ["_Command","SitChair"];
						 _agent setVariable ["Pose","SitChair"];
						 //sleep 8;
						 call _fnc_add_actionMenu;
						_running = false;
						_checkDone =true;
					};
					case ( "SetDirection" ): { // possible internal use only
						_dir = _destDirection;
						_agent setVariable ["_Parameters",[_dir] ];
						_agent setVariable ["_Command","SetDir"];
						_checkDone =true;
					};
					case ( "Goto" ): { 
						_agent setVariable ["_Parameters",[_destPosition] ];
						_agent setVariable ["_Command","Goto"];
						_checkDone =true;
					};
					default {
						sleep 0.5;
					};
				};
			} else {
				if (!_isIdle) then {
					_isIdle=true;
					#ifdef TES_AI_DEBUG
						_agent globalChat format ["Queue Empty"];
					#endif
				};
			};
		} else { sleep 1;};
	};
	sleep 0.2;
};

//_endpos = [(getPos _myObj select 0)  ,(getPos _myObj select 1)+ 0.8,0]; 